package au.chao.main;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import au.chao.utils.GraphUtility;
import au.chao.utils.Validator;

public class BillEnterForm extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3824662966289176054L;

	private JTable billTable;
	
	public static final int FLAG_ADD = 0;
	public static final int FLAG_EDIT = 1;
	
	private int addOrEdit = -1;
	String[] billList = BillDataLoader.loadBillType();
	final JComboBox lstBill = new JComboBox (billList);
	final JTextField txtCompanName = new JTextField();
	final JTextField txtStartDate = new JTextField();
	final JTextField txtEndDate = new JTextField();
	final JTextField txtFee = new JTextField();
	
	public BillEnterForm(int flag){
		super();
		addOrEdit = flag;
		intGUI();
	}
	
	public BillEnterForm(JTable tbl, int flag){
		super();
		billTable = tbl;
		addOrEdit = flag;
		intGUI();
	}
	
	void intGUI(){
		Container mainCnt = new Container();
		GridLayout lay = new GridLayout(2,16);
		lay.setHgap(10);
		lay.setVgap(10);
		mainCnt.setLayout(lay);
		
		JLabel lbBillType = new JLabel("Bill Type");
		JLabel lbCompanName = new JLabel("Company Name");
		JLabel lbFee = new JLabel("Fee");
		JLabel lbStartDate = new JLabel("Start Date");
		JLabel lbEndDate = new JLabel("End Date");
		
		mainCnt.add(lbBillType);
		mainCnt.add(lstBill);
		mainCnt.add(lbCompanName);
		mainCnt.add(txtCompanName);
		mainCnt.add(lbFee);
		mainCnt.add(txtFee);
		mainCnt.add(lbStartDate);
		mainCnt.add(txtStartDate);
		mainCnt.add(lbEndDate);
		mainCnt.add(txtEndDate);
		mainCnt.add(new JLabel());
		mainCnt.add(new JLabel());
		// Button container
		Container btnCnt = new Container( );
		FlowLayout flay = new FlowLayout();
		flay.setHgap(10);
		btnCnt.setLayout(flay);
		
		JButton btnAdd = new JButton("Save");
		btnAdd.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				if(addOrEdit == FLAG_ADD){
					Object[] billDetails = new Object[]{lstBill.getSelectedItem(), txtCompanName.getText(),
							txtStartDate.getText(), txtEndDate.getText(), txtFee.getText()};
					if(validInput(billDetails)){
						((BillTableModel)billTable.getModel()).addRow(billDetails);
						dispose();
					}
					else{
						JOptionPane.showMessageDialog(null, "Check Input Formate", "alert", JOptionPane.ERROR_MESSAGE); 
					}
				}
				else{
					int selectedRowNum = billTable.getSelectedRow();
					billTable.setValueAt(lstBill.getSelectedItem(),selectedRowNum, 0);
					billTable.setValueAt(txtCompanName.getText(),selectedRowNum, 1);
					billTable.setValueAt(txtStartDate.getText(),selectedRowNum, 2);
					billTable.setValueAt(txtEndDate.getText(),selectedRowNum, 3);
					billTable.setValueAt(txtFee.getText(),selectedRowNum, 4);
				}
				dispose();
			}
			
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				dispose();
			}
			
		});
		btnCnt.add(btnAdd);
		btnCnt.add(btnCancel);

		setLayout(new GridBagLayout());
		GridBagConstraints topCS = new GridBagConstraints();
		topCS.insets = new Insets(10,10,10,10);
		topCS.gridx = 0;
		topCS.gridy = 0;
		topCS.weightx = 1;
		topCS.weighty = 1;
		topCS.anchor = GridBagConstraints.PAGE_START;
		topCS.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(mainCnt,topCS);
		
		GridBagConstraints bottomCS = new GridBagConstraints();
		bottomCS.gridx = 0;
		bottomCS.gridy = 1;
		bottomCS.weightx = 1;
		bottomCS.weighty = 0;
		bottomCS.anchor = GridBagConstraints.PAGE_END;
		bottomCS.fill = GridBagConstraints.HORIZONTAL;
		bottomCS.insets = new Insets(10,10,10,10);
		getContentPane().add(btnCnt,bottomCS);
		
		setModal(true);
		pack();
		GraphUtility.centerWindow(this);

	}
	
	boolean validInput(Object [] input){
		if(((String)input[1]).length() <1)
			return false;
		if(! Validator.validDate((String)input[2])){
			return false;
		}
		if(! Validator.validDate((String)input[3])){
			return false;
		}
		if(! Validator.validNumber((String)input[4])){
			return false;
		}
		return true;
	}
	
	public void populateData() {
		if (billTable != null) {
			int selectedRowNum = billTable.getSelectedRow();
			String type = (String) ((BillTableModel) billTable
					.getModel()).getValueAt(selectedRowNum, 0);
			System.out.println(type);
			String compName = (String) ((BillTableModel) billTable
					.getModel()).getValueAt(selectedRowNum, 1);
			String startDate = (String) ((BillTableModel) billTable
					.getModel()).getValueAt(selectedRowNum, 2);
			String endDate = (String) ((BillTableModel) billTable
					.getModel()).getValueAt(selectedRowNum, 3);
			String bill = (String) ((BillTableModel) billTable.getModel())
					.getValueAt(selectedRowNum, 4);
			lstBill.setSelectedItem(type);
			txtCompanName.setText(compName);
			txtStartDate.setText(startDate);
			txtEndDate.setText(endDate);
			txtFee.setText(bill);
		}
	}
}
